package database.datastructure;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import antlr.debug.NewLineEvent;

import database.pojo.whereObject;

public class WhereImplementation {
	
	 Map<Integer, Map<String, String>> records = new HashMap<Integer, Map<String,String>>();
	 Map<Integer, Map<String,String>> resultSet = new HashMap<Integer, Map<String,String>>();
	
	public WhereImplementation(whereObject werObj){
		this.records = werObj.getRecords();
		String _where_expression = werObj.getExpression();
		checkExpression(_where_expression);
	}
	
	public void checkExpression(String exp)
	{
		if(exp.contains("and"))
		{
			Map<Integer, Map<String,String>> tempresultSet = new HashMap<Integer, Map<String,String>>();
			String expressions[] = exp.split("and");
			tempresultSet = evaluateExpression(expressions[0]);
			for(int i=1;i<expressions.length;i++)
			{
				Map<Integer, Map<String,String>> tempresultSet1 = evaluateExpression(expressions[1]);
				for(Integer key:tempresultSet1.keySet())
				{
					if(tempresultSet.containsKey(key)){
						resultSet.put(key, tempresultSet.get(key));						
					}
				}
			}
		}
		else if(exp.contains("or"))
		{			
			String expressions[] = exp.split("or");
			for(int i=0;i<expressions.length;i++)
			{
				Map<Integer, Map<String,String>> tempresultSet1 = evaluateExpression(expressions[i]);
				for(Integer key:tempresultSet1.keySet())
				{					
					resultSet.put(key, tempresultSet1.get(key));
				}
			}
		}
		else
		{
			resultSet = evaluateExpression(exp);			
		}
	}

	public HashMap<Integer, Map<String,String>> evaluateExpression(String exp){
		HashMap<Integer, Map<String,String>> tempresultSet = new HashMap<Integer, Map<String,String>>();
		if(exp.contains(">="))
		{
			int leftValue = 0, rightValue = 0;
			String expression[] = exp.split(">=");		
			String subExpressionLeft = expression[0];
			String subExpressionRight = expression[1];
			ExpressionEval exprEval = new ExpressionEval();

			for(Integer row_num:records.keySet())
			{
				leftValue = 0; rightValue = 0;
				Map<String, String> row = records.get(row_num);
				try
				{
					leftValue = Integer.parseInt(subExpressionLeft);				
				}catch(Exception e)
				{
					Map<String, Integer> conditionMapLeft =exprEval.evaluateExpression(subExpressionLeft);
					for(String temp:conditionMapLeft.keySet())
					{
						int arg1 = Integer.parseInt(row.get(temp));
						int arg2 = conditionMapLeft.get(temp);
						leftValue = leftValue + arg1 * arg2 ; 
					}					
				}

				try
				{
					rightValue = Integer.parseInt(subExpressionRight); 
				}catch(Exception e)
				{
					Map<String, Integer> conditionMapRight =exprEval.evaluateExpression(subExpressionRight);
					for(String temp:conditionMapRight.keySet())
					{
						int arg1 = Integer.parseInt(row.get(temp));
						int arg2 = conditionMapRight.get(temp);
						rightValue = rightValue + arg1 * arg2 ; 
					}					
				}

				if(leftValue >= rightValue)
				{
					tempresultSet.put(row_num, row);
				}
			}
		}
		else if(exp.contains("<=")){
			
			int leftValue = 0, rightValue = 0;
			String expression[] = exp.split("<=");		
			String subExpressionLeft = expression[0];
			String subExpressionRight = expression[1];
			ExpressionEval exprEval = new ExpressionEval();
			for(Integer row_num:records.keySet())
			{
				leftValue = 0; rightValue = 0;
				Map<String, String> row = records.get(row_num);
				try
				{
					leftValue = Integer.parseInt(subExpressionLeft);				
				}catch(Exception e)
				{
					Map<String, Integer> conditionMapLeft =exprEval.evaluateExpression(subExpressionLeft);
					for(String temp:conditionMapLeft.keySet())
					{
						int arg1 = Integer.parseInt(row.get(temp));
						int arg2 = conditionMapLeft.get(temp);
						leftValue = leftValue + arg1 * arg2 ; 
					}					
				}

				try
				{
					rightValue = Integer.parseInt(subExpressionRight); 
				}catch(Exception e)
				{
					Map<String, Integer> conditionMapRight =exprEval.evaluateExpression(subExpressionRight);
					for(String temp:conditionMapRight.keySet())
					{
						int arg1 = Integer.parseInt(row.get(temp));
						int arg2 = conditionMapRight.get(temp);
						rightValue = rightValue + arg1 * arg2 ; 
					}					
				}

				if(leftValue <= rightValue)
				{
					tempresultSet.put(row_num, row);
				}
			}
		}
		else if(exp.contains("!=")){
			int leftValue = 0, rightValue = 0;
			String expression[] = exp.split("!=");		
			String subExpressionLeft = expression[0];
			String subExpressionRight = expression[1];
			ExpressionEval exprEval = new ExpressionEval();
			for(Integer row_num:records.keySet())
			{
				leftValue = 0; rightValue = 0;
				Map<String, String> row = records.get(row_num);
				try
				{
					leftValue = Integer.parseInt(subExpressionLeft);				
				}catch(Exception e)
				{
					Map<String, Integer> conditionMapLeft =exprEval.evaluateExpression(subExpressionLeft);
					for(String temp:conditionMapLeft.keySet())
					{
						int arg1 = Integer.parseInt(row.get(temp));
						int arg2 = conditionMapLeft.get(temp);
						leftValue = leftValue + arg1 * arg2 ; 
					}					
				}

				try
				{
					rightValue = Integer.parseInt(subExpressionRight); 
				}catch(Exception e)
				{
					Map<String, Integer> conditionMapRight =exprEval.evaluateExpression(subExpressionRight);
					for(String temp:conditionMapRight.keySet())
					{
						int arg1 = Integer.parseInt(row.get(temp));
						int arg2 = conditionMapRight.get(temp);
						rightValue = rightValue + arg1 * arg2 ; 
					}					
				}

				if(leftValue != rightValue)
				{
					tempresultSet.put(row_num, row);
				}
			}
		}
		else if(exp.contains("<")){
			int leftValue = 0, rightValue = 0;
			String expression[] = exp.split("<");		
			String subExpressionLeft = expression[0];
			String subExpressionRight = expression[1];
			ExpressionEval exprEval = new ExpressionEval();
			for(Integer row_num:records.keySet())
			{
				leftValue = 0; rightValue = 0;
				Map<String, String> row = records.get(row_num);
				try
				{
					leftValue = Integer.parseInt(subExpressionLeft);				
				}catch(Exception e)
				{
					Map<String, Integer> conditionMapLeft =exprEval.evaluateExpression(subExpressionLeft);
					for(String temp:conditionMapLeft.keySet())
					{
						int arg1 = Integer.parseInt(row.get(temp));
						int arg2 = conditionMapLeft.get(temp);
						leftValue = leftValue + arg1 * arg2 ; 
					}					
				}

				try
				{
					rightValue = Integer.parseInt(subExpressionRight); 
				}catch(Exception e)
				{
					Map<String, Integer> conditionMapRight =exprEval.evaluateExpression(subExpressionRight);
					for(String temp:conditionMapRight.keySet())
					{
						int arg1 = Integer.parseInt(row.get(temp));
						int arg2 = conditionMapRight.get(temp);
						rightValue = rightValue + arg1 * arg2 ; 
					}					
				}

				if(leftValue < rightValue)
				{
					tempresultSet.put(row_num, row);
				}
			}
		}
		else if(exp.contains(">")){
			int leftValue = 0, rightValue = 0;
			String expression[] = exp.split(">");		
			String subExpressionLeft = expression[0];
			String subExpressionRight = expression[1];
			ExpressionEval exprEval = new ExpressionEval();
			for(Integer row_num:records.keySet())
			{
				leftValue = 0; rightValue = 0;
				Map<String, String> row = records.get(row_num);
				try
				{
					leftValue = Integer.parseInt(subExpressionLeft);				
				}catch(Exception e)
				{
					Map<String, Integer> conditionMapLeft =exprEval.evaluateExpression(subExpressionLeft);
					for(String temp:conditionMapLeft.keySet())
					{
						int arg1 = Integer.parseInt(row.get(temp));
						int arg2 = conditionMapLeft.get(temp);
						leftValue = leftValue + arg1 * arg2 ; 
					}					
				}

				try
				{
					rightValue = Integer.parseInt(subExpressionRight); 
				}catch(Exception e)
				{
					Map<String, Integer> conditionMapRight =exprEval.evaluateExpression(subExpressionRight);
					for(String temp:conditionMapRight.keySet())
					{
						int arg1 = Integer.parseInt(row.get(temp));
						int arg2 = conditionMapRight.get(temp);
						rightValue = rightValue + arg1 * arg2 ; 
					}					
				}


				if(leftValue > rightValue)
				{
					tempresultSet.put(row_num, row);
				}
			}
		}
		else if(exp.contains("=")){
			int leftValue = 0, rightValue = 0;
			String expression[] = exp.split("=");		
			String subExpressionLeft = expression[0];
			String subExpressionRight = expression[1];
			ExpressionEval exprEval = new ExpressionEval();
			for(Integer row_num:records.keySet())
			{
				leftValue = 0; rightValue = 0;
				Map<String, String> row = records.get(row_num);
				try
				{
					leftValue = Integer.parseInt(subExpressionLeft);				
				}catch(Exception e)
				{
					Map<String, Integer> conditionMapLeft =exprEval.evaluateExpression(subExpressionLeft);
					for(String temp:conditionMapLeft.keySet())
					{
						int arg1 = Integer.parseInt(row.get(temp));
						int arg2 = conditionMapLeft.get(temp);
						leftValue = leftValue + arg1 * arg2 ; 
					}					
				}

				try
				{
					rightValue = Integer.parseInt(subExpressionRight); 
				}catch(Exception e)
				{
					Map<String, Integer> conditionMapRight =exprEval.evaluateExpression(subExpressionRight);
					for(String temp:conditionMapRight.keySet())
					{
						int arg1 = Integer.parseInt(row.get(temp));
						int arg2 = conditionMapRight.get(temp);
						rightValue = rightValue + arg1 * arg2 ; 
					}					
				}

				if(leftValue == rightValue)
				{
					tempresultSet.put(row_num, row);
				}
			}
		}
		return tempresultSet;
	}
	
	public Map<Integer, Map<String,String>> returnResult()
	{
		return resultSet;
	}

}
